home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / byt0187b.arc / RGNMAKER.ASM < prev    next >
Assembly Source File  |  1980-01-01  |  22KB  |  1,102 lines

  1. ;  RgnMaker.ASM        Sun 23 Feb '86  h. katz
  2. ;            Tue 22 July
  3.  
  4.  
  5. XREF DoContour                ; source in < Traverse.ASM >
  6. XREF Save_To_File, PutFile_Posit    ; source in < SaveRgn.ASM >
  7. XREF Have_Prior_DITLs, ItemHit
  8.  
  9. XDEF GetFirstPixel, FormingRgn, StartCoords, Trav_Count, MyRowBytes
  10. XDEF IsRegion, CreateMenu, WMgrPort, ScratchSTR, PenPoint, WStorage
  11. XDEF Save_Image, Restore_Image, Stop_Alert, RgnHandle, UnHiLite
  12.  
  13. INCLUDE QuickEqu.D
  14.     ; portRect    equ    16    ; offset in Window Record
  15.     ; Bounds    equ    6    ; offsets into BitMap
  16.     ; rowBytes    equ    4
  17.     ; Top        equ    0    ; offsets into portRect & Bounds
  18.     ; Left        equ    2
  19.     ; Bottom    equ    4
  20.     ; Right        equ    6
  21.  
  22. StringToNum    equ    1    ; selector for _Pack7
  23.  
  24. scratch8    equ    $9FA
  25. scratch20    equ    $1E4
  26. ScreenBase    equ    $824
  27.  
  28. ScrapSize    equ    $960    ; ( word ) size in bytes
  29. ScrapHandle    equ    $964
  30. ScrapCount    equ    $968    ; ( word ) current counter value
  31. ScrapState    equ    $96A    ; ( word ) + = on disk
  32.                 ;  0 = in mem / - = not inited 
  33. ScrapName    equ    $96C    ;  StringPtr
  34.  
  35. CmdKey        equ    8    ; BitNum in Event Modifier Record
  36. PutFile_ID    equ    -3999    ; ResID for SFPutFile Dialog
  37.  
  38. WindID        equ    100
  39.  
  40. AppleMenu    equ    1
  41.  
  42. FileRgnMenu    equ    2
  43.   WriteRgn_Item    equ    1
  44.   Quit_Item    equ    3
  45.   
  46. EditRgnMenu    equ    3
  47.   CutItem    equ    3
  48.   CopyItem    equ    4
  49.   PasteItem    equ    5
  50.   
  51. CreateRgnMenu        equ    4
  52.   BuildRgnItem        equ    1
  53.   DontBuildItem        equ    2
  54.   CopyRegionItem    equ    4
  55.   AddItem        equ    5
  56.   SubtractItem        equ    6
  57.   
  58. DisplayRgnMenu    equ    5
  59. LastMenu    equ    5
  60.  
  61. FirstDACNum    equ    3
  62.  
  63. HiBitNum    equ    7    ; working with bytes
  64.  
  65. MouseDownEvent    equ    1
  66. KeyDownEvent    equ    3
  67.  
  68. Traverse_Cursor    equ    10    ; ResNum of my cursor
  69.  
  70.  
  71. INCLUDE MacTraps.D
  72.  
  73.     st    FormingRgn(a5)        ; turn off if we don't want to
  74.                     ; collect _Line's into a RgnDef
  75.     sf    DeskAcc_Opened(a5)
  76.     sf    Have_Pasted(a5)
  77.     st    First_Activate(a5)
  78.     sf    DoneFlag(a5)
  79.     sf    IsRegion(a5)        ; We've not done a Traverse with
  80.                     ;  'Form Region' On
  81.     sf    DoneCopyRegion(a5)    ; We've not copied a Region Traverse
  82.                     ;   to the Work Area
  83.     
  84.     sf    Have_Prior_DITLs(a5)    ; no prior saving of DITLs
  85.     move.l    #0, BitMap(a5)        ; haven't done a CopyBits yet
  86.     
  87.     BSR    InitManagers
  88.     BSR    Save_WMgrPort
  89.     BSR    Install_Menus
  90.     BSR    OpenWindow
  91.     
  92. EventLoop
  93.  
  94.     _SystemTask
  95.     
  96.     tst.b    Have_Pasted(a5)        ; if we haven't Pasted, 
  97.     beq    @1            ; leave the cursor alone
  98.     
  99.     clr.l    -(sp)
  100.     _FrontWindow            ; which window is frontmost ?
  101.     move.l    (sp)+, a0
  102.     lea    WStorage, a2        ; get Ptr to the Contour Window
  103.     cmpa.l    a0, a2
  104.     beq.s    @Check_pRect        ; Contour Window is in front
  105.  
  106.     bra.s    @1            ; some other window is in front
  107.     
  108. @Check_pRect
  109.  
  110.     clr    -(sp)        ; space for Boolean Func Result
  111.     pea    MouseLoc
  112.     _GetMouse        ; where's the cursor ?
  113.     move.l    MouseLoc, -(sp)
  114.     pea    portRect(a2)
  115.     _PtInRect
  116.     tst    (sp)+
  117.     beq.s    @1        ; NOT in contentRgn of Contour Window
  118.     
  119.     bsr    Set_Traverse_Cursor
  120.     bra.s    @2
  121.         
  122. @1    _InitCursor
  123.  
  124. @2    bsr    GetNextEvent
  125.     tst.b    DoneFlag(a5)
  126.     beq.s    EventLoop
  127.     _ExitToShell
  128.  
  129.  
  130. Set_Traverse_Cursor
  131.     
  132.     clr.l    -(sp)            ; reserve space for Handle
  133.     move    #Traverse_Cursor, -(sp)    ; Cursor ID
  134.     _GetCursor
  135.     move.l    (sp)+, a0
  136.     move.l    (a0), a0        ; ptr to CursorData
  137.     move.l    a0, -(sp)
  138.     _SetCursor
  139.     
  140.     RTS
  141.     
  142.  
  143. GetNextEvent
  144.  
  145.     subq.l    #2, sp
  146.     move    #-1, -(sp)        ; eventMask = everyEvent
  147.     pea    EventRecord
  148.     _GetNextEvent
  149.     tst.b    (sp)+
  150.     beq.s    Return
  151.     move    What, D0
  152.     beq.s    Return
  153.     cmp    #9, d0            ; don't worry about events
  154.     bge.s    Return            ; numbered 9 or higher
  155.     
  156.     add    d0, d0
  157.     lea    EventJTable, a0
  158.     add    0(a0, d0), a0
  159.     jmp(a0)                ; we RTS out of each routine
  160.                     ; to EventLoop
  161.                     
  162. EventJTable
  163.  
  164. @NullEvent    dc    Return        - EventJTable    ; 0
  165. @MDown        dc    MouseDown     - EventJTable    ; 1
  166. @MouseUp    dc    Return        - EventJTable    ; 2
  167. @KDown        dc    KeyDown        - EventJTable    ; 3
  168. @KeyUp        dc    Return        - EventJTable    ; 4
  169. @AutoKey    dc    Return        - EventJTable    ; 5
  170. @Update        dc    Return        - EventJTable    ; 6
  171. @Disk        dc    Return        - EventJTable    ; 7
  172. @Activate    dc    Activate    - EventJTable    ; 8
  173. @Undefined    dc    Return        - EventJTable    ; 9
  174.  
  175.  
  176. Activate
  177.  
  178.     ; check if the Contour Window is coming active.  If it is,
  179.     ; and this is the first time here, try Pasting in from the Scrap.
  180.     ; else check if a Desk Accessory was open just prior.  If it was,
  181.     ; restore the saved background
  182.  
  183.     clr.l    -(sp)
  184.     _FrontWindow            ; which window is frontmost ?
  185.     move.l    (sp)+, a0
  186.     lea    WStorage, a2        ; get Ptr to the Contour Window
  187.     cmpa.l    a0, a2
  188.     bne.s    @Rts            ; Contour Window
  189.         
  190. @0    tst.b    First_Activate(a5)    ; do an 'auto-Paste' on 1st Activate
  191.     beq.s    @1            ; no
  192.     sf    First_Activate(a5)    ; yes
  193.     BRA    Paste_From_Clip
  194.     
  195. @1    tst.b    DeskAcc_Opened(a5)    ; was User using the ScrapBook ?
  196.     beq.s    @Rts            ; no
  197.     
  198.     sf    DeskAcc_Opened(a5)    ; yes - turn off flag
  199.  
  200.     BSR    Restore_Image        ; restore old background 
  201.  
  202. @Rts    RTS
  203.     
  204.     
  205. MouseDown
  206.  
  207.     clr    -(sp)        ; returns where Mouse was clicked
  208.     move.l    Where, -(sp)    ; global coords of Mouse Location
  209.     pea    WhichWindow    ; whose ?
  210.     _FindWindow
  211.     move    (sp)+, d0    ; where was it
  212.     add    d0, d0
  213.     lea    MouseJTable, a0
  214.     add    0(a0, d0), a0
  215.     jmp(a0)
  216.  
  217. Return    
  218.  
  219.     RTS                ; return to EventLoop
  220.     
  221. MouseJTable
  222.  
  223. @inDesk        dc    Return        - MouseJTable
  224. @inMBar     dc    InMenu        - MouseJTable
  225. @SysEvent    dc    SystemEvent    - MouseJTable
  226. @Content    dc    InContent    - MouseJTable
  227. @Drag        dc    Return        - MouseJTable
  228. @Grow        dc    Return        - MouseJTable
  229. @GoAway        dc    TrackGoAway    - MouseJTable
  230.     
  231.     
  232. Save_Image
  233.  
  234.     ; first get the intersection of the current port and the
  235.     ; incoming Dialog or Alert in Global Coords.  
  236.     ; ResID of Resource in D3 / ResType in A3
  237.     
  238.     lea    WStorage+portRect, a0
  239.     lea    Scratch20, a2
  240.     move.l    (a0)+, (a2)+        ; move Window pRect into 1st 8
  241.     move.l    (a0), (a2)+        ; bytes of Scratch20 area
  242.     pea    Scratch20
  243.     _LocalToGlobal
  244.     pea    Scratch20+4
  245.     _LocalToGlobal
  246.     
  247.     clr.l    -(sp)            ; returned Handle
  248.     move.l    a3, -(sp)        ; 'DLOG' or 'ALRT'
  249.     move    d3, -(sp)        ; DLOG or ALRT ResID
  250.     _GetResource
  251.     move.l    (sp)+, d1        ; Handle
  252.     BEQ    @ErrRts
  253.     
  254. @1    move.l    d1, a0
  255.     move.l    (a0), a0        ; Ptr to Data
  256.     
  257.     move.l    (a0)+, (a2)+        ; move pRect of DLOG into 2nd 8 bytes
  258.     move.l    (a0), (a2)        ; of Scratch20 area
  259.     
  260.     cmp    #PutFile_ID, d3
  261.     bne.s    @2
  262.     
  263.     ; SFPutFile DLOG requires an adjustment for PutFile_Posit
  264.     
  265.     pea    Scratch20+8
  266.     move.l    PutFile_Posit, -(sp)    ; Global TopLeft of DLOG pRect
  267.     _OffsetRect
  268.     
  269. @2    pea    Scratch20+8        ; compensate for Window Frame
  270.     move    #-8, -(sp)        ; around the DLOG/ALRT pRect
  271.     move    #-8, -(sp)
  272.     _InsetRect
  273.     
  274.     clr    -(sp)            ; BOOLEAN result
  275.     pea    Scratch20        ; Contour Window ( Global )
  276.     pea    Scratch20+8        ; Dialog ( Global )
  277.     pea    Scratch20+8        ; => Intersection ( Global )
  278.     _SectRect
  279.     tst    (sp)+
  280.     BEQ    @ErrRts    
  281.     
  282.     pea    Scratch20+8
  283.     _GlobalToLocal
  284.     pea    Scratch20+8+4
  285.     _GlobalToLocal
  286.     
  287.     lea    BitMap(a5), a3
  288.     move.l    Scratch20+8,  Bounds(a3)
  289.     move.l    Scratch20+12, Bounds+4(a3)
  290.     
  291.     BSR    Get_Size        ; returns D0 = Size ( bytes )
  292.                     ;         D1 = rowBytes                    
  293.     move    d1, rowBytes(a3)
  294.     
  295.     _NewHandle,CLEAR
  296.     BMI    @ErrRts
  297.     
  298.     move.l    a0, Image_Handle(a5)
  299.     move.l    (a0), (a3)        ; deref to Ptr = BaseAddr
  300.                     ;  in ToBitMap
  301.     move.l    (a5), a2        ; QDVars
  302.     move.l    (a2), a2
  303.     lea    2(a2), a2
  304.         
  305.     move.l    a2, -(sp)        ; fromBitMap
  306.     move.l    a3, -(sp)        ; toBitMap
  307.     
  308.     pea    Scratch20+8        ; local coords of fromRect
  309.     move.l    (sp), -(sp)        ; again for ToRect
  310.     move    #0, -(sp)        ; mode
  311.     clr.l    -(sp)            ; maskRgn = NIL
  312.     _CopyBits
  313.     BRA    @Rts
  314.     
  315.     
  316. @ErrRts    move.l    #0, BitMap(a5)        ; can't save the image - forget it
  317.     
  318. @Rts    RTS
  319.  
  320.  
  321. Restore_Image
  322.  
  323.     pea    WStorage
  324.     _BeginUpdate
  325.     
  326.     tst.l    BitMap(a5)        ; have we done a 'Save_Image' CopyBits ?
  327.     beq.s    @End            ; no
  328.         
  329.     lea    BitMap(a5), a2
  330.     move.l    Image_Handle(a5), a0
  331.     move.l    (a0), (a2)        ; baseAddr
  332.  
  333.     move.l    a2, -(sp)        ; fromBitMap
  334.     
  335.     move.l    (a5), a3
  336.     move.l    (a3), a3
  337.     pea    2(a3)            ; toBitMap ( the screen )
  338.     
  339.     pea    Bounds(a2)
  340.     move.l    (sp), -(sp)        ; again for ToRect
  341.     move    #0, -(sp)        ; mode
  342.     clr.l    -(sp)            ; maskRgn = NIL
  343.     _CopyBits
  344.     
  345.     move.l    #0, BitMap(a5)        ; clear CopyBits 'flag'
  346.     move.l    Image_Handle(a5), a0
  347.     _DisposHandle            ; clear up space on the Heap
  348.     
  349. @End    pea    WStorage
  350.     _EndUpdate
  351.  
  352.     RTS
  353.  
  354.  
  355. Get_Size
  356.  
  357.     move    Bounds+Right(a3), d2
  358.     ext.l    d2
  359.     sub    Bounds+Left(a3), d2    ; width
  360.     add    #1, d2
  361.     divu    #16, d2            ; => remainder + quotient
  362.     move    d2, d1            ; save quotient
  363.     and.l    #$FFFF0000, d2        ; check remainder
  364.     beq.s    @1            ; no remainder
  365.     add    #1, d1            ; rowWords
  366.     
  367. @1    add    d1, d1            ; words -> bytes
  368.     move    Bounds+Bottom(a3), d0
  369.     sub    Bounds+Top(a3), d0
  370.     add    #1, d0
  371.                     ; d1 => rowBytes
  372.     mulu    d1, d0            ; d0 => Size ( long ) for _NewHandle
  373.     
  374.     RTS
  375.     
  376.     
  377. TrackGoAway
  378.  
  379.     clr    -(sp)            ; space for BOOLEAN result
  380.     pea    WStorage
  381.     move.l    Where, -(sp)        ; from Event Record
  382.     _TrackGoAway
  383.     move    (sp)+, d0
  384.     bne    SetDone            ; mouse WAS released in goAway box
  385.     RTS                ; it wasn't
  386.     
  387. InMenu
  388.  
  389.     clr.l    -(sp)
  390.     move.l    Where, -(sp)        ; global again
  391.     _MenuSelect            ; track the mouse in the MBar
  392.     move.l    (sp)+, d0        ; save Menu and ItemNum
  393.     swap    d0            ; MenuNum -> LowByte
  394.     
  395. FindMenu
  396.  
  397.     cmp    #AppleMenu, d0
  398.     BEQ    In_Apple_Menu
  399.     cmp    #FileRgnMenu, d0
  400.     BEQ    In_File_Menu
  401.     cmp    #EditRgnMenu, d0
  402.     BEQ    In_Edit_Menu
  403.     cmp    #CreateRgnMenu, d0
  404.     BEQ    In_CreateRgn_Menu
  405.     cmp    #DisplayRgnMenu, d0
  406.     BEQ    In_DisplayRgn_Menu
  407.     BRA    UnHiLite
  408.     
  409. In_Apple_Menu
  410.     
  411.     swap    d0        ; ItemNum back in Low Byte    
  412.     cmp    #1, d0
  413.     bne    @GetDAC        ; not Item 1 - must be Open a Desk Acc
  414.     
  415.     move    #107, d3    ; ResID of upcoming 'About' DLOG
  416.     move.l    #'DLOG', a3
  417.     BSR    Save_Image
  418.     
  419.     clr.l    -(sp)        ; reserve space for Ptr
  420.     move    #106, -(sp)    ; 'About' DLOG ID
  421.     move.l    #0, -(sp)    ; create space
  422.     move.l    #-1, -(sp)    ; in front of everything
  423.     _GetNewDialog
  424.     move.l    (sp)+, d4
  425.  
  426.     BSR    @Wait_for_next
  427.         
  428.     move.l    d4, -(sp)
  429.     _CloseDialog
  430.     
  431.     clr.l    -(sp)        ; reserve space for Ptr
  432.     move    #107, -(sp)    ; 'About' DLOG Instructions ID
  433.     move.l    #0, -(sp)    ; create space
  434.     move.l    #-1, -(sp)    ; in front of everything
  435.     _GetNewDialog
  436.     move.l    (sp)+, d4
  437.  
  438.     BSR    @Wait_for_next
  439.     
  440.     move.l    d4, -(sp)
  441.     _CloseDialog
  442.     
  443.     clr.l    -(sp)        ; reserve space for Ptr
  444.     move    #108, -(sp)    ; 'About' DLOG Instructions Part2 ID
  445.     move.l    #0, -(sp)    ; create space
  446.     move.l    #-1, -(sp)    ; in front of everything
  447.     _GetNewDialog
  448.     move.l    (sp)+, d4
  449.  
  450.     BSR    @Wait_for_next
  451.     
  452.     move.l    d4, -(sp)
  453.     _CloseDialog
  454.  
  455.     BSR    Restore_Image
  456.     
  457.     bra.s    UnHiLite
  458.  
  459. @Wait_for_next
  460.  
  461.     move.l    #0, -(sp)    ; no filterProc
  462.     pea    ItemHit
  463.     _ModalDialog
  464.     
  465.     move    ItemHit, d0
  466.     cmp    #1, d0
  467.     bne    @Wait_for_next
  468.     
  469.     RTS    
  470.  
  471. @GetDAC
  472.  
  473.     move.l    HAppleMenu(a5), -(sp)    ; saved menuHandle for AppleMenu
  474.     move    d0, -(sp)
  475.     pea    DACName
  476.     _GetItem
  477.     
  478.     st    DeskAcc_Opened(a5)
  479.     
  480.     move    #WindID, d3        ; ResID of upcoming Dialog/Alert
  481.     move.l    #'WIND', a3
  482.     BSR    Save_Image        ; save Bits to be hidden by the DeskAcc
  483.  
  484.     clr    -(sp)
  485.     pea    DACName
  486.     _OpenDeskAcc
  487.     move    (sp)+, d0
  488.     
  489.     pea    WStorage
  490.     _SetPort
  491.  
  492. UnHiLite
  493.     clr    -(sp)
  494.     _HiLiteMenu
  495.     RTS
  496.  
  497.     
  498. In_File_Menu
  499.  
  500.     swap    D0
  501.     cmp    #Quit_Item, D0
  502.     BEQ    SetDone
  503.     cmp    #WriteRgn_Item, d0
  504.     bne    UnHiLite
  505.     
  506.     BSR    Save_To_File    ; see source in < SaveRgn.ASM >
  507.     
  508.     bra    UnHiLite
  509.  
  510.  
  511. In_Edit_Menu
  512.  
  513.     swap    d0            ; Put ItemNum in Low Byte
  514.     move    d0, d3
  515.     BSR    System_Edit
  516.     BNE.s    @Bra            ; Desk Acc handled the Menu selection
  517.     move    d3, d0            ; restore MenuID & ItemNum
  518.     
  519.     cmp    #CutItem, d0
  520.     BEQ.s    @Cut
  521.     cmp    #CopyItem, d0
  522.     BEQ.s    @Copy
  523.     cmp    #PasteItem, d0
  524.     BNE.s    @Bra
  525.     
  526. ; do a Paste
  527.  
  528.     BRA    Paste_From_Clip    
  529.     
  530. @Cut
  531.  
  532.     BRA    @Bra
  533.     
  534. @Copy
  535.  
  536. @Bra    BRA    UnHiLite
  537.  
  538.  
  539. System_Edit
  540.  
  541.     sub    #1, d0            ; check if the Desk Accessory is
  542.     clr    -(sp)            ; going to handle our Edit selection
  543.     move    d0, -(sp)
  544.     _SysEdit
  545.     move    (sp)+, d0        ; pop the result
  546.     RTS                ; ( FALSE = WE handle it )
  547.     
  548.     
  549. In_DisplayRgn_Menu
  550.  
  551.     swap    d0
  552.     cmp    #1, d0            ; Clear Window ?
  553.     beq.s    @clear
  554.     
  555.     tst.b    DoneCopyRegion(a5)    ; Don't Allow Region Operations if
  556.     BEQ    @bra1            ;  we haven't copied one to Work Area
  557.     
  558.     cmp    #3, d0            ; Frame Region ?
  559.     beq.s    @Frame
  560.     cmp    #4, d0            ; Paint Region ?
  561.     beq.s    @Paint
  562.     cmp    #5, d0            ; Invert Region ?
  563.     beq.s    @Invert
  564.     cmp    #7, d0
  565.     beq.s    @DrawSize
  566.     BRA    @Bra1
  567.  
  568. @Frame    move.l    RgnHandle(a5), -(sp)
  569.     _FrameRgn
  570.     bra.s    @Bra1
  571.     
  572. @Paint    move.l    RgnHandle(a5), -(sp)
  573.     _PaintRgn
  574.     bra.s    @Bra1
  575.     
  576. @Invert    move.l    RgnHandle(a5), -(sp)
  577.     _InverRgn
  578.     bra.s    @Bra1
  579.     
  580. @clear    move.l    (a5), a0
  581.     move.l    (a0), a0
  582.     pea    16(a0)
  583.     _EraseRect
  584.     bra.s    @Bra1
  585.     
  586. @DrawSize
  587.     
  588.     move.l    WMgrPort, -(sp)
  589.     _SetPort
  590.     move.l    PenPoint, -(sp)
  591.     _MoveTo
  592.     pea    '        '
  593.     _DrawString
  594.     
  595.     move.l    RgnHandle(a5), a0    ; Handle
  596.     move.l    (a0), a0        ; Pointer to ( Addr of ) Region
  597.     move    (a0), d0        ; RgnSize ( INT )
  598.         
  599.     ext.l    d0
  600.     lea    ScratchSTR, a0
  601.     move    #0, -(sp)        ; Num to String
  602.     _Pack7
  603.     pea    ScratchSTR
  604.     _DrawString
  605.     pea    '  Bytes'
  606.     _DrawString
  607.     
  608.     pea    WStorage
  609.     _SetPort
  610.     
  611.     
  612. @bra1    BRA    UnHiLite
  613.  
  614.  
  615. SetDone
  616.     st    DoneFlag(a5)
  617.     RTS
  618.     
  619.     
  620. In_CreateRgn_Menu
  621.  
  622.     swap    D0
  623.     move    D0, D3            ; Save Selected Item Number
  624.     
  625.     cmp    #DontBuildItem, d0
  626.     bhi    @2            ; Was it 'Copy' or greater ?
  627.  
  628. ; User Selected either 'Build Region' or 'Display Only'
  629.  
  630.     move.l    CreateMenu(A5), -(sp)    ; UnCheck Both Items 1 & 2
  631.     move    #1, -(sp)
  632.     sf    -(sp)
  633.     _CheckItem
  634.     move.l    CreateMenu(A5), -(sp)
  635.     move    #2, -(sp)
  636.     sf    -(sp)
  637.     _CheckItem
  638.     
  639.     move.l    CreateMenu(A5), -(sp)    ; and Check the Apt Item
  640.     move    d3, -(sp)
  641.     st    -(sp)
  642.     _CheckItem
  643.     
  644.     cmp    #DontBuildItem, d3
  645.     bne.s    @1            ; We're ARE Forming a Region
  646.     
  647.     sf    FormingRgn(A5)        ; We're NOT Forming a Region    
  648.     BRA    @Bra
  649.  
  650. @1    st    FormingRgn(A5)
  651.     BRA    @Bra
  652.     
  653. ; User Selected either 'Copy to', 'Add to', or 'Subtract from Work Area'
  654.     
  655. @2    tst.b    IsRegion(a5)
  656.     BEQ    @Bra            ; We haven't Formed a Region yet
  657.     
  658.     cmp    #CopyRegionItem, d3
  659.     beq.s    @CopyToWorkArea
  660.     
  661.     tst.b    DoneCopyRegion(a5)
  662.     BEQ    @Bra            ; We can't Add or Subtract from the
  663.                     ;  Work Area until we've done a 'Copy'
  664.     cmp    #AddItem, d3
  665.     beq.s    @AddToWorkArea
  666.     cmp    #SubtractItem, d3
  667.     beq.s    @SubtractFromWorkArea
  668.     
  669.     BRA    @Bra
  670.     
  671. @CopyToWorkArea
  672.  
  673.     tst.b    DoneCopyRegion(a5)    ; have we previously done a 'Copy' ?
  674.     beq.s    @3            ; no
  675.     
  676.     move.l    RgnHandle(a5), -(sp)    ; we don't want old Regions cluttering
  677.     _DisposRgn            ; up the Heap
  678.     
  679. @3    move.l    a6, -(sp)        ; Handle to Current (traverse) Region
  680.     clr.l    -(sp)
  681.     _NewRgn
  682.     move.l    (sp), RgnHandle(a5)
  683.     _CopyRgn
  684.         
  685.     st    DoneCopyRegion(a5)    ; use this flag to allow
  686.                     ; later Add & Subtract
  687.     move.l    FileMenu(A5), -(sp)
  688.     move    #WriteRgn_Item, -(sp)
  689.     _EnableItem
  690.  
  691.     move.l    CreateMenu(A5), -(sp) 
  692.     move.l    (sp), -(sp)
  693.     move    #AddItem, -(sp)        ; And allow these 2 selections
  694.     _EnableItem
  695.     move    #SubtractItem, -(sp)
  696.     _EnableItem
  697.  
  698.     move.l    DisplayMenu(A5), -(sp)
  699.     move.l    (sp), -(sp)
  700.     move.l    (sp), -(sp)
  701.     move.l    (sp), -(sp)
  702.     move    #3, -(sp)        ; Frame Region
  703.     _EnableItem    
  704.     move    #4, -(sp)        ; Paint Region
  705.     _EnableItem
  706.     move    #5, -(sp)        ; Invert Region
  707.     _EnableItem
  708.     move    #7, -(sp)        ; Draw Region Size
  709.     _EnableItem
  710.  
  711.     BRA    @Bra
  712.     
  713. @AddToWorkArea
  714.  
  715.     move.l    a6, -(sp)        ; SourceA = Current (traverse) Region
  716.     move.l    RgnHandle(a5), -(sp)    ; SourceB = Destination (Work Area) Rgn
  717.     move.l    RgnHandle(a5), -(sp)    ; Destination = Work Area
  718.     _UnionRgn
  719.  
  720.     BRA.s    @Bra
  721.     
  722. @SubtractFromWorkArea
  723.  
  724.     move.l    RgnHandle(a5), -(sp)    ;   Region B
  725.     move.l    a6, -(sp)        ; - Region A
  726.     move.l    RgnHandle(a5), -(sp)
  727.     _DiffRgn
  728.     
  729. @Bra    BRA    UnHiLite
  730.     
  731.     
  732. SystemEvent
  733.  
  734.     pea    EventRecord
  735.     move.l    WhichWindow, -(sp)
  736.     _SystemClick
  737.     RTS
  738.  
  739.  
  740. InContent        
  741.  
  742.     tst.b    Have_Pasted(a5)        ; have we got anything to traverse ?
  743.     beq    Return            ; no
  744.         
  745.     bra    DoContour        ; ( returns from DoContour to EventLoop )
  746.     
  747.     
  748. KeyDown
  749.     ; check to see if the Command Key was down
  750.     ; if so, see it it's a menu-item equivalent
  751.     ; else ignore it
  752.     
  753.     move    Modifiers, d3
  754.     btst    #CmdKey, d3
  755.     bNE    @GetCmdKey
  756.     
  757.     RTS
  758.     
  759. @GetCmdKey
  760.  
  761.     clr.l    -(sp)
  762.     move    Message+2, -(sp)    ; get the character
  763.     _MenuKey
  764.     move.l    (sp)+, d0
  765.     swap    d0            ; put MenuID in Low Byte
  766.     
  767.     BRA    FindMenu
  768.     
  769.  
  770. Paste_From_Clip
  771.  
  772.     ; check the scrap.  if there's no PICT there, just
  773.     ; beep and return for now.  else frame it in the window
  774.     
  775.     move.l    ScrapHandle, d0
  776.     beq    @8            ; Beep no Scrap
  777.         
  778.     ; we've got a 'PICT' on the Clipboard
  779.     
  780.     move.l    #0, d0
  781.     _NewHandle
  782.     bmi    ErrReturn
  783.     
  784.     move.l    a0, a2            ; save the handle
  785.     clr.l    -(sp)
  786.     move.l    a2, -(sp)        ; destination handle for incoming
  787.     move.l    #'PICT', -(sp)        ; PICT
  788.     pea    Offset
  789.     _GetScrap
  790.     tst.l    (sp)+            ; byte count or OS ErrCode
  791.     bmi    @8            ; Beep No Data of Type in Scrap
  792.     
  793.     move.l    a2, -(sp)        ; Handle to PICTure ( used in 3rd call )
  794.     move.l    a2, a6            ; save the Handle
  795.     
  796.     move.l    (a2), a2        ; Ptr to PICT Resource
  797.     lea    2(a2), a2        ; Ptr to PicFrame
  798.     lea    Scratch8, a3
  799.     move.l    (a2)+, (a3)+        ; A4 =  PicFrame.TopLeft
  800.     move.l    (a2), (a3)        ;        .BottomRight
  801.     lea    Scratch20, a4
  802.     lea    WStorage, a2
  803.     move.l    portRect+Left(a2), d3    ; pRect.LeftBottom
  804.     add.l    #$000AFFF6, d3        ; move PICT Org over ( 10, -10 )
  805.     move.l    d3, (a4)
  806.     
  807.     move.l    -2(a3), -(sp)        ; PicFrame.Left
  808.     pea    (a4)            ; pRect.LeftBottom  = destPoint
  809.     _SubPt
  810.     
  811.     move.l    (a4), d0
  812.     swap    d0
  813.     move.l    d0, (a4)
  814.     
  815.     pea    Scratch8
  816.     move.l    (a4), -(sp)
  817.     _OffsetRect
  818.     
  819.     pea    WStorage+portRect    ; erase any old PICTs
  820.     _EraseRect
  821.     
  822.     pea    -4(a3)            ; destRect = picFrame rectangle        
  823.     _DrawPicture
  824.     
  825.     move.l    a6, -(sp)        ; free up some space in the heap
  826.     _KillPicture
  827.     
  828.     tst.b    Have_Pasted(a5)        ; reset to our Cursor if 1st time
  829.     bne.s    @9            ; not 1st time
  830.     
  831.     st    Have_Pasted(a5)
  832.     bra    @9
  833.     
  834. @8    move    #2, -(sp)        ; Beep if couldn't Paste
  835.     _SysBeep
  836.  
  837. @9    BRA     UnHiLite
  838.  
  839. ErrReturn
  840.     _Debugger
  841.     
  842. Offset    dc.l    0            ; used if we need most 'important'
  843.                     ; version of Type    
  844.     
  845. GetFirstPixel    
  846.  
  847.     ; user points w/mouse to a point just to the left of any
  848.     ; left-edged pixel ( no 6-Neighbor ) in the region to be traversed.
  849.     ; Program then scans left to right to find the apt byte and BitNum.
  850.  
  851.     ; < Where > is global coords of point to left of ON pixel in Rgn
  852.                 
  853.     ; now calculate byte offset to first pixel in block from
  854.     ; global address
  855.  
  856.  
  857.     pea    Where
  858.     _GlobalToLocal
  859.     lea    WStorage, a1        ; set up Right Bounds to check
  860.     move    portRect+Right(a1), d3    ; against ( else we erase the
  861.     sub    #4, d3            ; Window Frame )
  862.                     ; and leave a little leeway
  863. @CheckNextPixel
  864.  
  865.     move.l    Where, -(sp)
  866.     _MoveTo
  867.     clr.b    -(sp)
  868.     move.l    Where, -(sp)
  869.     _GetPixel
  870.     
  871.     tst.b    (sp)+
  872.     bne.s    @2            ; we found one !
  873.     lea    Where, a0
  874.     add    #1, 2(a0)        ; move horizontally 1
  875.     cmp    2(a0), d3        ; have we passed the right edge ?
  876.     bpl.s    @CheckNextPixel        ; no        
  877.  
  878. @1    _ShowCursor            ; yes - we got problems
  879.  
  880.     move    #102, d3        ; StrID for upcoming Alert
  881.     BSR    Stop_Alert        ; 'Can't locate first point'
  882.     
  883.     RTS            
  884.  
  885.  
  886. @2    move.l    Where, StartCoords(a5)
  887.                 ; save the local Where for an _OpenRgn
  888.                 ; _MoveTo command & for closing the loop
  889.                 
  890.     pea    Where        ; now that we've found the local coords of
  891.     _LocalToGlobal        ; our first pixel, convert that to an
  892.     move.l    Where, d0    ; absolute memory reference ( Addr + BitNum )
  893.  
  894.     ; get byte addr    ( a1 ) and bitNum of point ( d1 )
  895.     
  896.     swap    d0            ; y in Low Word
  897.     move    d0, d1
  898.     mulu    #64, d1            ; number of bytes down
  899.     swap    d0            ; x back in Low Word
  900.     and.l    #$0000FFFF, d0        ; zero Hi Word
  901.     divu    #8, d0            ; number of bytes over
  902.     add    d0, d1
  903.     add.l    ScreenBase, d1
  904.     move.l    d1, A3            ; = < Addr of StartPt >
  905.     
  906.     swap    d0            ; get back remainder in pixels
  907.     and.l    #$0000FFFF, d0        ; zero quotient
  908.     sub.b    #7, d0
  909.     neg     d0
  910.     and.l    #7, d0
  911.     
  912.     move    d0, D3            ; = < BitNum of StartPt >
  913.     move    #-1, d0            ; Neg Flag = Found One
  914.     
  915.     RTS
  916.     
  917.     
  918. Stop_Alert    ; an Alert of some sort is coming up
  919.         ; the ID of the String DITL is in D3
  920.         
  921.     move.l    #'ALRT', a3
  922.     BSR    Param_Text
  923.     move    #101, d3    ; ResID for all StopAlerts
  924.     BSR    Save_Image
  925.     
  926.     _InitCursor        ; reset to the standard northwest arrow
  927.         
  928.     clr    -(sp)
  929.     move    d3, -(sp)    ; AlertID
  930.     move.l    #0, -(sp)
  931.     _StopAlert
  932.     move    (sp)+, d0
  933.     
  934.     BSR    Restore_Image
  935.     
  936.     RTS
  937.  
  938.  
  939. Param_Text
  940.  
  941.     clr.l    -(sp)
  942.     move    d3, -(sp)
  943.     _GetString
  944.     move.l    (sp)+, a0
  945.     move.l    (a0), -(sp)    ; ^0
  946.     move.l    #0, -(sp)    ; ^1
  947.     move.l    #0, -(sp)    ; ^2
  948.     move.l    #0, -(sp)    ; ^3
  949.     _ParamText
  950.     
  951.     RTS
  952.     
  953.     
  954. OpenWindow
  955.  
  956.     clr.l    -(sp)        ; for returned WindowPtr
  957.     move    #WindID, -(sp)    ; WindowID
  958.     pea    WStorage    ; storage for Window Record
  959.     move.l    #-1, -(sp)    ; in front
  960.     _GetNewWindow
  961.     _SetPort
  962.     lea    WStorage, a0
  963.     pea    portRect(a0)    ; push the addr of portRect
  964.     _ClipRect
  965.     RTS
  966.  
  967.  
  968. Save_WMgrPort
  969.  
  970.     move.l    (a5), a0
  971.     move.l    (a0), a0    ; thePort
  972.     lea    WMgrPort, a1
  973.     move.l    a0, (a1)
  974.     move    ScreenBits+rowBytes(a0), MyRowBytes(a5)
  975.     RTS
  976.  
  977.     
  978. Install_Menus
  979.  
  980.     clr.l    -(sp)
  981.     move    #AppleMenu, -(sp)
  982.     _GetRMenu            ; resNum of DeskAcc Menu
  983.     move.l    (sp), HAppleMenu(a5)    ; save MenuHandle for later
  984.     move.l    (sp), -(sp)        ; push copy for _AddResMenu
  985.     clr    -(sp)            ; append to end
  986.     _InsertMenu
  987.     
  988.     move.l    #'DRVR', -(sp)
  989.     _AddResMenu
  990.     
  991.     clr.l    -(sp)            ; Note:  would have been a bit more
  992.     move    #FileRgnMenu, -(sp)    ; elegant to store these handles
  993.     _GetRMenu            ; in an array rather than separate
  994.     move.l    (sp), FileMenu(a5)    ; variables.  Oh well, if it works ...
  995.     clr    -(sp)
  996.     _InsertMenu
  997.  
  998.     clr.l    -(sp)
  999.     move    #EditRgnMenu, -(sp)
  1000.     _GetRMenu
  1001.     move.l    (sp), EditMenu(a5)
  1002.     clr    -(sp)
  1003.     _InsertMenu    
  1004.     
  1005.     clr.l    -(sp)
  1006.     move    #CreateRgnMenu, -(sp)
  1007.     _GetRMenu
  1008.     move.l    (sp), CreateMenu(a5)
  1009.     clr    -(sp)
  1010.     _InsertMenu
  1011.     
  1012.     clr.l    -(sp)
  1013.     move    #DisplayRgnMenu, -(sp)
  1014.     _GetRMenu
  1015.     move.l    (sp), DisplayMenu(a5)
  1016.     clr    -(sp)
  1017.     _InsertMenu
  1018.         
  1019.     _DrawMenuBar
  1020.     
  1021.     move.l    CreateMenu(A5), -(sp)
  1022.     move    #BuildRgnItem, -(sp)    ; check it
  1023.     st    -(sp)
  1024.     _CheckItem
  1025.     
  1026.     RTS
  1027.     
  1028.     
  1029. InitManagers
  1030.  
  1031.     pea        -4(a5)
  1032.     _InitGraf
  1033.     _InitFonts
  1034.     _InitWindows
  1035.     _InitMenus
  1036.     clr.l    -(sp)
  1037.     _InitDialogs
  1038.     _TEInit
  1039.     _InitCursor
  1040.     move.l    #$FFFF0000, d0
  1041.     _FlushEvents
  1042.     RTS
  1043.     
  1044.  
  1045. ;  CONSTANTS ( PC-rel addressing )  ----------------------------------
  1046.     
  1047. EventRecord
  1048.    What:    dc.w    0
  1049.    Message:    dc.l    0
  1050.    When:    dc.l    0
  1051.    Where:    dc.l    0
  1052.    Modifiers:    dc.w    0
  1053.  
  1054. PenPoint    dc.l    0        ; start of Rgn Size Display
  1055. MouseLoc    dc.l    0        ; for _PtInRect cursor check
  1056.  
  1057. ScratchSTR    dcb.b    10, 0
  1058.   
  1059. WStorage        dcb.b    156, 0
  1060.  
  1061. DACName        dcb.b    40, 0
  1062.  
  1063. WhichWindow    dc.l    0
  1064. WMgrPort    dc.l    0
  1065.  
  1066. boundsRect    dc.w    45, 10, 335, 500
  1067.  
  1068. StandardProcs    dcb.l    13, 0
  1069.  
  1070.  
  1071. ;  VARs ( ref'd off A5 )  ------------------------------------------
  1072.  
  1073. Trav_Count    ds    1
  1074.  
  1075. Default_Vol    ds    1    ; VolRefNum of Default Volume
  1076. CurResFile    ds    1    ; RefNum of Current Res File
  1077.  
  1078. MyRowBytes    ds.l    1    ; used in Traverse.ASM / copy of rowBytes
  1079. RgnHandle    ds.l    1
  1080. StartCoords    ds.l    1
  1081. Image_Handle    ds.l    1    ; Handle to saved Bit Image
  1082.  
  1083. HAppleMenu    ds.l    1    ; handles for menus
  1084. FileMenu    ds.l    1
  1085. EditMenu    ds.l    1
  1086. CreateMenu    ds.l    1
  1087. DisplayMenu    ds.l    1
  1088.  
  1089. BitMap        ds.b    14
  1090.  
  1091. DeskAcc_Opened    ds.b    1    ; User has opened the ScrapBook DeskAcc
  1092. First_Activate    ds.b    1    ; for Pasting from the Scrap on 1st Activate
  1093. Have_Pasted    ds.b    1    ; have done a Paste
  1094. DoneFlag    ds.b    1    ; GoAwayBox Click or File Menu 'Quit'
  1095. FormingRgn    ds.b    1    ; Build / Don't Build a Region during Traverse
  1096. IsRegion    ds.b    1    ; we've done a Traverse w/ 'Form Region' On
  1097. DoneCopyRegion    ds.b    1    ; we've done a 'Copy Region' to the Work Area
  1098.  
  1099.     END
  1100.     
  1101.     
  1102.